﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using EzDesk.Core.ServiceImpl.Services;
using EzDesk.Utility.Log;
using EzDesk.Utility.ServiceStatusInfo;

namespace EzDesk.ServiceHost.Controllers
{
    public class LoginController : EzDeskAppController
    {
        private CoreClientLoginService _loginService = new CoreClientLoginService();
        private CoreClientService _clientService = new CoreClientService();
        private CoreAppService _coreAppService = new CoreAppService();
      

        /// <summary>
        /// AuthToken方式登录系统
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public JsonResult Auth(string userName, string password, string deviceToken)
        {
            var userProfile = _loginService.Login(userName, password);
            var statusInfo = new StatusInfo();
            if (userProfile != null)
            {
                var token = TokenService.RequestToken(userProfile.Id);
                statusInfo.Data = new
                {
                    UserProfie = userProfile,
                    Token = token,
                };
                Response.Cookies.Add(new HttpCookie("ez_auth_token", token));

                if (deviceToken != null)
                {
                    //保存deviceToken
                    _coreAppService.SaveDeviceToken(userProfile.Id, deviceToken);
                }
            }
            else
            {
                statusInfo.Status = ServiceStatus.Error;
            }

            if (_clientService.CheckOutOfPeriod())
            {
                statusInfo.Status = ServiceStatus.Error;
            }


            return Json(statusInfo);
        }
        
        //
        // POST: /SignOut/
        // RETURN_TYPE: StatusInfo

        /// <summary>
        /// 用户退出
        /// </summary>
        /// <returns></returns>
        /// 
        [EzAuthorize]
        public JsonResult SignOut()
        {
            var statusInfo = new StatusInfo();
            try
            {
                // 清空手机端的 device_token
                _coreAppService.ClearDeviceToken(CurrentUserId);
                // 清空客户端cookie
                Response.Cookies.Remove("ez_auth_token");
            }
            catch (Exception)
            {
                statusInfo.Status = ServiceStatus.Error;
            }
                
            return Json(statusInfo);
        }


        //
        // GET: /Login/UserNameForLogin
        // todo: 需要的时候再实现

        /// <summary>
        /// 通过一个输入的字符串获得用户的合法登录名的方法
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public JsonResult UserNameForLogin(string inputString)
        {
            throw  new NotImplementedException();
        }

    }
}
